<?php
// +----------------------------------------------------------------------
// | 知识星球[基于ThinkPHP5开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 abcblockchain.io
// +----------------------------------------------------------------------
// | 后台管理使用了HisiPHP
// +----------------------------------------------------------------------
// | Author: abcblockchain.io
// +----------------------------------------------------------------------
namespace app\api\home;

use app\common\controller\Api;
use app\common\model\Group as groupModel;
use app\common\model\GroupPosts as postsModel;
use app\common\model\Comments as commentsModel;
use app\common\model\Likes as likesModel;
use think\Db;

class GroupPosts extends Api
{
    protected $groupModel;
    protected $postsMode;
    protected $commentsModel;
    public function __construct()
    {
        parent::__construct();
        $this->groupModel = new groupModel();
        $this->postsMode = new postsModel();
        $this->commentsModel = new commentsModel();
    }


    /**
     * 创建主题
     * @author
     */
    public function createPost(){
        $postData = $this->requestData;
        if(isset($postData['group_id']) && $postData['group_id'] > 0 && isset($postData['user_id']) && $postData['user_id'] > 0) {
            $groupUser = $this->groupModel->groupUser($postData['group_id'],$postData['user_id']);
            if($groupUser){
                $resValid = $this->validate($postData, 'GroupPosts.create');
                if($resValid !== true){
                    $result = array(
                        'status' => 0,
                        'data'   => 'create failed',
                    );
                    if($resValid == '1'){
                        $result['code'] = 5;
                    }elseif($resValid == '2'){
                        $result['code'] = 6;
                    }elseif($resValid == '3'){
                        $result['code'] = 7;
                    }
                    echo json_encode($result);
                    exit;
                }
                $res = $this->postsMode->createPosts($postData);
                if($res){
                    $result = array(
                        'status' => 1,
                        'data'   => 'create success',
                        'code'   => 0,
                    );
                }else{
                    $result = array(
                        'status' => 0,
                        'data'   => 'create failed',
                        'code'   => 4,
                    );
                }
            }else{
                $result = array(
                    'status' => 0,
                    'data'   => 'not in group',
                    'code'   => 8,
                );
            }
        }else{
            $result = array(
                'status' => 0,
                'data'   => 'param error',
                'code'   => 1,
            );
        }
        echo json_encode($result);
        exit();
    }

    /**
     * 评论主题
     * @author
     */
    public function postsComments(){
        $postData = $this->requestData;
        $resValid = $this->validate($postData,'Comments.create');
        if($resValid !== true){
            $result = array(
                'status' => 0,
                'data'   => 'comment failed',
            );
            if($resValid == '1'){
                $result['code'] = 5;
            }elseif($resValid == '2'){
                $result['code'] = 6;
            }elseif($resValid == '3'){
                $result['code'] = 7;
            }elseif($resValid == '4'){
                $result['code'] = 8;
            }
            echo json_encode($result);
            exit;
        }else{
            $res = $this->commentsModel->createComments($postData);
            if($res){
                $result = array(
                    'status' => 1,
                    'data'   => 'comment success',
                    'code'   => 0,
                );
            }else{
                $result = array(
                    'status' => 0,
                    'data'   => 'comment success',
                    'code'   => 4,
                );
            }
            echo json_encode($result);
            exit;
        }
    }

    /**
     * 发现页的内容
     * @author
     */
    public function findPost(){
        $postData = $this->requestData;
        if(isset($postData['userId']) && $postData['userId'] > 0) {

            $userGroup = $this->groupModel->getUserGroup($postData['userId']);
            if($userGroup){
                $page = isset($postData['nowPage']) ? $postData['nowPage'] : 0;
                $postList = $this->postsMode->findPost($userGroup,$page);
            }else{
                $postList = array();
            }
            $result = array(
                'status' => 1,
                'data'   => $postList,
                'code'   => 0,
            );
        }else{
            $result = array(
                'status' => 0,
                'data'   => 'param error',
                'code'   => 1,
            );
        }
        echo json_encode($result);
        exit();
    }

    /**
     * 主题点赞
     * @author
     */
    public function postLike(){
        $postData = $this->requestData;
        if(isset($postData['postId']) && $postData['postId'] > 0 && isset($postData['userId']) && $postData['userId'] > 0) {
            $likesModel = new likesModel();
            $res = $likesModel->createLikes($postData['postId'],$postData['userId']);
            if($res){
                $result = array(
                    'status' => 1,
                    'data'   => 'success',
                    'code'   => 0,
                );
            }else{
                $result = array(
                    'status' => 0,
                    'data'   => 'failed',
                    'code'   => 2,
                );
            }
        }else{
            $result = array(
                'status' => 0,
                'data'   => 'param error',
                'code'   => 1,
            );
        }
        echo json_encode($result);
        exit();
    }


    public function cancelLike(){
        $postData = $this->requestData;
        if(isset($postData['postId']) && $postData['postId'] > 0 && isset($postData['userId']) && $postData['userId'] > 0) {
            $likesModel = new likesModel();
            $res = $likesModel->cancelLikes($postData['postId'],$postData['userId']);
            if($res){
                $result = array(
                    'status' => 1,
                    'data'   => 'success',
                    'code'   => 0,
                );
            }else{
                $result = array(
                    'status' => 0,
                    'data'   => 'failed',
                    'code'   => 2,
                );
            }
        }else{
            $result = array(
                'status' => 0,
                'data'   => 'param error',
                'code'   => 1,
            );
        }
        echo json_encode($result);
        exit();
    }

    /**
     * 帖子详情
     * @author
     */
    public function postDetail(){
        $postData = $this->requestData;
        if(isset($postData['postId']) && $postData['postId'] > 0) {
            $postInfo = $this->postsMode->postDetail($postData['postId']);
            if($postInfo){
                $result = array(
                    'status' => 1,
                    'data'   => $postInfo,
                    'code'   => 0,
                );
            }else{
                $result = array(
                    'status' => 0,
                    'data'   => 'no data',
                    'code'   => 4,
                );
            }
        }else{
            $result = array(
                'status' => 0,
                'data'   => 'param error',
                'code'   => 1,
            );
        }
        echo json_encode($result);
        exit();
    }

    /**
     * 获取评论列表
     * @author
     */
    public function getCommentList(){
        $postData = $this->requestData;
        if(isset($postData['postId']) && $postData['postId'] > 0) {
            $nowPage = isset($postData['nowPage']) ? $postData['nowPage'] : 0;
            $commentList = $this->commentsModel->getReply($postData['postId'],$nowPage);
            if($commentList){
                $result = array(
                    'status' => 1,
                    'data'   => $commentList,
                    'code'   => 0,
                );
            }else{
                $result = array(
                    'status' => 1,
                    'data'   => [],
                    'code'   => 0,
                );
            }
        }else{
            $result = array(
                'status' => 0,
                'data'   => 'param error',
                'code'   => 1,
            );
        }
        echo json_encode($result);
        exit();
    }
}